// pages/setBankAccount/setBankAccount.js
import imgUrl from "../wallet/imgUtil.js"
import { getPhoneCode ,bindBankCard ,updateBankCard} from "../../ajax/module/wallets.js"
import { validateAddForm,backCardFormat } from "./validate.js"
let app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    province:"",
    rightIcon:imgUrl.rightIcon,
    bankCard:{},

    code:"",
    num:60,
    phone:"",

    // code:"",
    text:"发送验证码",
    flg:true,


    region:["省份","城市"],
    activeBank:"11",
    bankNode:"",
    bankCode:"",

    bankCodeFormart:"",
    // 账户名称
    bankName:""
  },
  RegionChange(e) {
    console.log(e)
    this.setData({
      region: e.detail.value
    })
  },
  jumpBankType(){
    wx.navigateTo({
      url: '../bankType/bankType',
    })
  },

  listenActiveBank(e){
    this.setData({
      activeBank: e.detail.value
    })
  },
  listenBankName(e){
    this.setData({
      bankName: e.detail.value.replace(/[^\u4E00-\u9FA5]/g,'')
    })
  },
  
  codeListen(e){
      this.setData({
        code:e.detail.value
      })
  },
  getCode(){
    if(this.data.flg){
      
      getPhoneCode({phone:this.data.phone}).then(res=>{
          wx.showToast({
            title: '获取验证码成功',
          })
          this.setData({
            flg:false,
        
          })
           this.interval(()=>{
             
            this.setData({
              flg:true,
              text:"重新发送"
            })

           })
      })
    }
  },
  interval(callback){
    //  let time = 60 
    let time=60
    let temp= setInterval(()=>{
      time=time-1
      this.setData({
        num:time
      })
      if(time == 0){
        clearInterval(temp)
        callback()
      }
    },1000)
  },


  phoneListen(e){
    this.setData({
      phone:e.detail.value
    })
  },

  listenBankNode(e){
    this.setData({
      bankNode:e.detail.value.replace(/[^\u4E00-\u9FA5]/g,''),
     
    })
  },

  listenBankCode(e){
    this.setData({
      bankCode:e.detail.value,
      bankCodeFormart:backCardFormat(e.detail.value)  
    })
  },

  save(){
    //  this.data.region[0]
    //  this.data.region[1]
    //  this.data.activeBank
    //  this.data.bankNode
    //  this.data.bankCode
    //  this.data.bankName
    //  this.data.phone
    //  this.data.code
    

    if(this.data.id == 2) {
       let data ={ 
        bankName: this.data.activeBank,
        branchName: this.data.bankNode,
        cardHolder: this.data.bankName,
        cardNo: this.data.bankCode,
        city: this.data.region[1],      
        province: this.data.region[0],      
        smsCode: this.data.code,
        phone:this.data.phone,
        bankId:this.data.bankId,
        isDefault:"1"
       }
       console.log(data)
       validateAddForm(data,()=>{
          

          bindBankCard(data).then(res=>{
            console.log("嘿嘿",res.data)
            wx.showToast({
              title: '设置账户成功',
            })
            setTimeout(()=>{
               wx.navigateBack()
            },1500)
          })


       })
      



    }
    if(this.data.id == 1) {
      // updateBankCard()
      let data ={ 
        bankName: this.data.activeBank,
        branchName: this.data.bankNode,
        cardHolder: this.data.bankName,
        cardNo: this.data.bankCode,
        city: this.data.region[1],      
        province: this.data.region[0],      
        smsCode: this.data.code,
        phone:this.data.phone,
        bankId:this.data.bankId,
        bankCardId:app.globalData.tempBankCard.bankCardId,
        isDefault:app.globalData.tempBankCard.isDefault
       }
       

      validateAddForm(data,()=>{
        updateBankCard(data).then(res=>{
          console.log("嘿嘿",res.data)
          wx.showToast({
            title: '修改账户成功',
          })
          setTimeout(()=>{
             wx.navigateBack()
          },1500)
        })


     })
    }




  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      /**
       * id = 1 修改银行
       * id = 2 新增银行
       */
      console.log(options.id)
      this.setData({
         id:options.id
      })
      if(options.id == 1){
        // 处理 编辑 银行账户
        wx.getStorage({
          key: 'loginName',
          success:res=>{
            this.setData({
              phone:res.data
            })
          }
        })
        console.log(app.globalData.tempBankCard)
        this.setData({
          bankCard:app.globalData.tempBankCard,
          
          // activeBank:app.globalData.tempBankCard.bankName,
          activeBank:app.globalData.tempBankCard.bankName,
          bankId:app.globalData.tempBankCard.bankType,
          bankNode:app.globalData.tempBankCard.branchName,
          bankCode:app.globalData.tempBankCard.cardNo,
          bankCodeFormart:backCardFormat(app.globalData.tempBankCard.cardNo),
          bankName:app.globalData.tempBankCard.cardHolder,
        })




      }
      if(options.id ==2){
        wx.getStorage({
          key: 'loginName',
          success:res=>{
            this.setData({
              phone:res.data
            })
          }
        })
       
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    //  console.log("9999", this.data.id)
      // e.currentTarget.dataset.item
      // 处理 从银行类型 页面 跳转 设置账户 页面
      let selectBank =  app.globalData.tempBankActive 
      this.setData({
        activeBank:selectBank.bankName||"",
        bankId:selectBank.bankId||""
      })
      if(this.data.id == 1) {
        this.setData({
          activeBank:app.globalData.tempBankCard.bankName,
          bankId:app.globalData.tempBankCard.bankType
        })
      }
      

    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})